/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.wutu.nbyjk8.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wutu.nbyjk8.domain.CarPassThroughRecord;
import com.wutu.nbyjk8.mapper.CarPassThroughRecordMapper;
import com.wutu.nbyjk8.vo.EntryData;
import com.wutu.nbyjk8.vo.ExitData;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 车辆通行记录Service
 *
 * @author 朱欢乐
 * @version 2025-09-25
 */
@Service
@Transactional
public class CarPassThroughRecordService extends ServiceImpl<CarPassThroughRecordMapper, CarPassThroughRecord> {


    public void receiveEntry(EntryData entryData) throws ParseException {
        // 处理进场数据逻辑
        CarPassThroughRecord carPassThroughRecord = new CarPassThroughRecord();
        BeanUtils.copyProperties(entryData, carPassThroughRecord);
        carPassThroughRecord.setIotype("1");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date parse = simpleDateFormat.parse(entryData.getItime());
        carPassThroughRecord.setItime(parse);
        carPassThroughRecord.setInserttime(new Date());
        baseMapper.insert(carPassThroughRecord);
    }

    public void receiveExit(ExitData exitData) throws ParseException {
        // 处理出场数据逻辑
        CarPassThroughRecord carPassThroughRecord = new CarPassThroughRecord();
        BeanUtils.copyProperties(exitData, carPassThroughRecord);
        carPassThroughRecord.setIotype("2");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date in = simpleDateFormat.parse(exitData.getItime());
        carPassThroughRecord.setItime(in);
        final Date out = simpleDateFormat.parse(exitData.getOtime());
        carPassThroughRecord.setOtime(out);
        final long between = DateUtil.between(in, out, DateUnit.MINUTE);
        if (between > 0) {
            final BigDecimal divide = new BigDecimal(between).divide(new BigDecimal(60), 2, RoundingMode.UP);
            carPassThroughRecord.setPtime(Double.valueOf(divide.toString()));
        }
        carPassThroughRecord.setMon(String.valueOf(exitData.getMon()));
        carPassThroughRecord.setCardType(exitData.getCardType());
        carPassThroughRecord.setInserttime(new Date());
        baseMapper.insert(carPassThroughRecord);
    }

}
